﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using Ample.Model;

namespace Ample.DAL.User
{
    public static class BoardQueries
    {
        public static readonly Func<AmpleDataContext, string, IEnumerable<BoardPosition>> CompiledQuery_GetPostionsByLevel =
           CompiledQuery.Compile<AmpleDataContext, string, IEnumerable<BoardPosition>>((adc, level) =>

                   from o in adc.BoardPositions where o.Level == level
                   select o
           );

        public static readonly Func<AmpleDataContext, bool, IEnumerable<Board>> compiledQuery_GetBoardsByOpen =
          CompiledQuery.Compile<AmpleDataContext, bool, IEnumerable<Board>>((adc, isOpen) =>

                  from o in adc.Boards
                  where o.IsOpen == isOpen
                  select o
          );

        public static readonly Func<AmpleDataContext, IEnumerable<Board>> compiledQuery_GetBoards =
          CompiledQuery.Compile<AmpleDataContext, IEnumerable<Board>>((adc) =>
                  from o in adc.Boards
                  select o
          );

        public static readonly Func<AmpleDataContext, string, IEnumerable<Board>> compiledQuery_GetBoardsByCode =
          CompiledQuery.Compile<AmpleDataContext, string, IEnumerable<Board>>((adc, code) =>
                  from o in adc.Boards
                  where o.BoardCode == code
                  select o
          );

        public static readonly Func<AmpleDataContext, int, IEnumerable<Board>> compiledQuery_GetBoardsById =
          CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<Board>>((adc, id) =>
                  from o in adc.Boards
                  where o.BoardId == id
                  select o
          );

        public static readonly Func<AmpleDataContext, int, IEnumerable<BoardUser>> compiledQuery_GetChildBoardUsersByBoardUserId =
         CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<BoardUser>>((adc, boardUserId) =>
                 from o in adc.BoardUsers
                 where o.ParentBoardUserId == boardUserId
                 select o
         );

        public static readonly Func<AmpleDataContext, int, IEnumerable<TreeBoardUser>> compiledQuery_GetTreeBoardUsersByBoardId =
          CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<TreeBoardUser>>((adc, id) =>
                  from o in adc.BoardUsers
                  join or in adc.Orders on o.OrderId equals or.OrderId
                  where o.BoardId == id && o.Approved == true
                  select new TreeBoardUser{BoardUserName = or.UserName, PositionCode = o.PositionCode, Id = o.BoardUserId.ToString(), ParentId = o.ParentBoardUserId.Value.ToString()} 
          );

        public static readonly Func<AmpleDataContext, int, IEnumerable<BoardUser>> compiledQuery_GetBoardUsersByBoardId =
          CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<BoardUser>>((adc, id) =>
                  from o in adc.BoardUsers
                  join or in adc.Orders on o.OrderId equals or.OrderId
                  where o.BoardId == id
                  select o
          );

        public static readonly Func<AmpleDataContext, int, IEnumerable<BoardUser>> compiledQuery_GetBoardWaitingUsersByBoardId =
         CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<BoardUser>>((adc, id) =>
                 from o in adc.BoardUsers
                 join or in adc.Orders on o.OrderId equals or.OrderId
                 where o.BoardId == id & o.Approved == false
                 select o
         );

        public static readonly Func<AmpleDataContext, int, IEnumerable<BoardUser>> compiledQuery_GetParentBoardUserByBoardUserId =
         CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<BoardUser>>((adc, id) =>
                 from o in adc.BoardUsers
                 where o.BoardUserId == id
                 select o.ParentBoardUser
         );

        public static readonly Func<AmpleDataContext, int, IEnumerable<BoardUser>> compiledQuery_GetBoardUserByBoardUserId =
         CompiledQuery.Compile<AmpleDataContext, int, IEnumerable<BoardUser>>((adc, id) =>
                 from o in adc.BoardUsers
                 where o.BoardUserId == id
                 select o
         );

        public static readonly DataLoadOptions BoardUserOptions =
            (new Func<DataLoadOptions>(() =>
            {
                var option = new DataLoadOptions();

                option.LoadWith<BoardUser>(b => b.Order);
                option.LoadWith<BoardUser>(b => b.Board);
                option.LoadWith<Order>(o => o.User);
                
                return option;
            }))();

        public static readonly DataLoadOptions BoardOptions =
            (new Func<DataLoadOptions>(() =>
            {
                var option = new DataLoadOptions();

                option.LoadWith<Board>(b => b.BoardUsers);
                
                return option;
            }))();
    }
}
